package com.chuantou.yuba.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.chuantou.yuba.R;
import com.chuantou.yuba.api.to.NewsTO;
import com.chuantou.yuba.util.BitmapBinder;
import com.chuantou.yuba.util.ViewHolder;

/**
 * 资讯列表Adapter
 * 
 * @author dl1001
 * 
 */
public class NewsListAdapter extends BaseAdapter {
	private List<NewsTO> mNews = new ArrayList<NewsTO>();
	private Context mContext;

	public NewsListAdapter(Context context) {
		this.mContext = context;
	}

	public void addNewsAd(List<NewsTO> list) {
		if (list != null && !list.isEmpty()) {
			for (NewsTO to : list) {
				if (!mNews.contains(to)) {
					mNews.add(to);
				}
			}
		}
	}

	public void clear() {
		mNews.clear();
	}

	@Override
	public int getCount() {
		return mNews.size();
	}

	@Override
	public Object getItem(int position) {
		return mNews.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = View.inflate(mContext, R.layout.news_list_item, null);
		}
		NewsTO newsTO = mNews.get(position);
		NetworkImageView imageView = ViewHolder.get(convertView, R.id.image);
		BitmapBinder.bindNetImage(mContext, imageView, newsTO.getImage(), R.drawable.default_image_small, false);
		TextView titleView = ViewHolder.get(convertView, R.id.title);
		titleView.setText(newsTO.getTitle());
		TextView subTitleView = ViewHolder.get(convertView, R.id.sub_title);
		subTitleView.setText(newsTO.getSubTitle());
		// TextView sourceView = ViewHolder.get(convertView, R.id.source);
		// sourceView.setText(newsTO.getSource());
		TextView commentCountView = ViewHolder.get(convertView, R.id.comment_count);
		commentCountView.setText(mContext.getString(R.string.comment_count_format, newsTO.getReplyCount()));
		return convertView;
	}
}